package com.uchengwang.ui.cm_ui.cycle.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.entity.MapRequest;
import com.uchengwang.functions.http.listener.OnResultListener;
import com.uchengwang.ui.UChengApplication;
import com.uchengwang.ui.base_ui.ui.BaseWithTitleActivity;
import com.uchengwang.ui.cm_ui.cycle.bean.BottomBean;
import com.uchengwang.ui.cm_ui.cycle.http.CycleHttpUtils;
import com.uchengwang.ui.utils.DialogUtils;
import com.uchengwang.ui.utils.PopupUtils;
import com.uchengwang.ui.utils.UCommonUtil;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by U城网 on 2018/6/21.
 * 申请达人
 */

public class ApplyTalentActivity extends BaseWithTitleActivity implements View.OnClickListener{

    private RelativeLayout apply_talent_rl, apply_school_rl, apply_code_rl;
    private TextView apply_talent_name,apply_school, apply_code;
    private Button apply_ok;

    private String codeString;
    private long cycleItemId;
    private String cycleItemName;
    private List<BottomBean> beanList;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_apply_talent);
        setTitle("申请达人");

        cycleItemId = getIntent().getLongExtra(BaseConfig.CYCLE_ITEM_ID, 0);
        cycleItemName = getIntent().getStringExtra(BaseConfig.CYCLE_ITEM_NAME);

        initView();
    }

    private void initView(){
        apply_talent_rl = (RelativeLayout) findViewById(R.id.apply_talent_rl);
        apply_talent_rl.setOnClickListener(this);

        apply_school_rl = (RelativeLayout) findViewById(R.id.apply_school_rl);

        apply_code_rl = (RelativeLayout) findViewById(R.id.apply_code_rl);
        apply_code_rl.setOnClickListener(this);

        apply_talent_name = (TextView) findViewById(R.id.apply_talent_name);
        apply_school = (TextView) findViewById(R.id.apply_school);
        apply_code = (TextView) findViewById(R.id.apply_code);
        apply_ok = (Button) findViewById(R.id.apply_ok);
        apply_ok.setOnClickListener(this);

        apply_school.setText(UChengApplication.mySchoolName);

        if (cycleItemId >0 && !TextUtils.isEmpty(cycleItemName)){
            apply_talent_name.setText(cycleItemName);
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.apply_talent_rl:
                if (beanList ==null){
                    beanList = new ArrayList<>();
                    beanList.add(new BottomBean(true, "音乐", 0, R.color.comment_color, R.drawable.shape_top_left_right_white, 21));
                    beanList.add(new BottomBean(true, "街舞", 0, R.color.comment_color, R.drawable.shape_bottom_left_right_white, 12));

                }
                PopupUtils.getUtils().bottom(this, beanList, false, getRy(), new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        PopupUtils.getUtils().dismiss();
                        cycleItemId = beanList.get(position).getId();
                        cycleItemName = beanList.get(position).getName();

                        apply_talent_name.setText(cycleItemName);
                    }
                });

                break;
            case R.id.apply_code_rl:
                DialogUtils.getUtils().showEditWindow(this, "邀请码", codeString, 10, new OnResultListener() {
                    @Override
                    public void onResult(int stutas, String msg, int total, String data) {
                        if (!TextUtils.isEmpty(data)){
                            codeString = data;
                            apply_code.setText(data);
                        }
                    }
                });
                break;
            case R.id.apply_ok:
                if (TextUtils.isEmpty(cycleItemName)){
                    UCommonUtil.getUtil().showToast(this, "请选择主题");
                    return;
                }
                MapRequest map = new MapRequest();
                map.put(BaseConfig.CYCLE_ITEM_ID, cycleItemId);
                map.put(BaseConfig.CYCLE_ITEM_NAME, cycleItemName);
                map.put(BaseConfig.INVITE_CODE, codeString);
                map.put(BaseConfig.UNI_ID, UChengApplication.myUniId);
                map.put(BaseConfig.UNI_NAME, UChengApplication.mySchoolName);

                CycleHttpUtils.getUtils().authTalent(this, map, new OnResultListener() {
                    @Override
                    public void onResult(int stutas, String msg, int total, String data) {
                        if (stutas ==0){
                            UCommonUtil.getUtil().showToast(ApplyTalentActivity.this, "申请成功");
                            EventBus.getDefault().post(BaseConfig.APPLY_TALENT_SUCCESS);
                            ApplyTalentActivity.this.finish();
                        }else {
                            UCommonUtil.getUtil().showToast(ApplyTalentActivity.this, "请重试");
                        }
                    }
                });
                break;
        }
    }
}
